package com.wenli.graduate2022.wenliGreenShop.repository.mapper;

import com.wenli.graduate2022.wenliGreenShop.entity.PropertyValue;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.springframework.stereotype.Repository;

import java.util.List;

/**属性值类
 * @author HuangHai
 * @date 2021/10/11 10:29
 */
@Repository
public interface PropertyValueMapper {

    @Results(id = "propertyValue",value = {  //写在findByPassword方法上会默认使用该封装规则，其他方法要明确调用该方法才能使用该规则
            @Result(property = "propertyValueProperty",column = "propertyvalue_property_id",
                    one = @One(select = "com.wenli.graduate2022.wenliGreenShop.repository.mapper.PropertyMapper.findById",
                            fetchType = FetchType.LAZY)), //查出对应哪个属性
            @Result(property = "propertyValueProduct",column = "propertyvalue_product_id",
                    one = @One(select ="com.wenli.graduate2022.wenliGreenShop.repository.mapper.ProductMapper.findById",
                            fetchType = FetchType.LAZY)) //查出是哪个产品
    })
    @Select("select * from propertyValue where propertyvalue_product_id = #{id}")
    List<PropertyValue> findByProductId(Integer id);

    @Insert("insert into propertyValue(propertyvalue_value,propertyvalue_property_id,propertyvalue_product_id) value(#{propertyvalueValue},#{propertyValueProperty.propertyId},#{propertyValueProduct.productId})")
    @Options(useGeneratedKeys = true, keyProperty = "propertyvalueId",keyColumn = "propertyvalue_id")
    @ResultMap("propertyValue")
    int insert(PropertyValue propertyValue);

    /**
     * 修改属性值
     * @param propertyValue
     * @param id
     * @return
     */
    @Update("update propertyValue set propertyvalue_value = #{propertyValue} where propertyvalue_id = #{id}")
    @ResultMap("propertyValue")
    int update(String propertyValue,Integer id);
}
